<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="会议终端"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	    <a-input v-model="queryForm.name" placeholder="设备名称" allow-clear @input="tableSearch">
	      <template #prefix><icon-search /></template>
	    </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #custom-right>
        <a-button type="primary" @click="onSyncEquipment">
          <template #icon><icon-sync /></template>
          <template #default>同步设备</template>
        </a-button>
        <!-- <a-button v-permission="['generator:equipment:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button> -->
      </template>
      <template #userType="{ record }">
        <span v-if="record.userType === 12">智慧屏</span>
        <span v-else-if="record.userType === 13">IdeaHub</span>
        <span v-else-if="record.userType === 14">SmartRooms</span>
        <span v-else-if="record.userType === 25">第三方会议终端</span>
      </template>
      <template #factoryName="{ record }">
        <span v-if="record.snCode === 'UNSFDSH7F9EONL7OMHXN'">北京富创</span>
        <span v-else>{{ record.factoryName }}</span>
      </template>
      <template #buildingName="{ record }">
        <span v-if="record.snCode === 'UNSFDSH7F9EONL7OMHXN'">二号地</span>
        <span v-else>{{ record.buildingName }}</span>
      </template>
      <template #meetingRoomName="{ record }">
        <span v-if="record.snCode === 'UNSFDSH7F9EONL7OMHXN'">3-8</span>
        <span v-else>{{ record.meetingRoomName }}</span>
      </template>
      <template #status="{ record }">
        <span v-if="record.status === 0">正常</span>
        <span v-else-if="record.status === 1">停用</span>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link
            v-permission="['system:equipment:updateName']"
            title="修改"
            :disabled="record.snCode === 'UNSFDSH7F9EONL7OMHXN'"
            @click="onUpdate(record)"
          >
            修改
          </a-link>
          <!-- <a-link
            v-permission="['generator:equipment:delete']"
            status="danger"
            :disabled="record.disabled"
            title="删除"
            @click="onDelete(record)"
          >
            删除
          </a-link> -->
        </a-space>
      </template>
    </GiTable>

    <EquipmentAddModal ref="EquipmentAddModalRef" @save-success="search" @update-success="onUpdateSuccess" />
    <EquipmentSyncModal ref="EquipmentSyncModalRef" @save-success="search" />
  </div>
</template>

<script setup lang="ts">
import { debounce } from 'lodash-es'
import { Message } from '@arco-design/web-vue'
import EquipmentAddModal from './EquipmentAddModal.vue'
import EquipmentSyncModal from './EquipmentSyncModal.vue'
import { type EquipmentQuery, type EquipmentResp, deleteEquipment, listEquipment, updateEquipmentName } from '@/apis/organization'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'Equipment' })

const queryForm = reactive<EquipmentQuery>({
  name: '',
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listEquipment({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: 'ideahub华为云账号', dataIndex: 'userAccount', slotName: 'userAccount', width: 160 },
  { title: '设备名称', dataIndex: 'name', slotName: 'name', width: 220 },
  { title: '所属厂区', dataIndex: 'factoryName', slotName: 'factoryName', width: 110 },
  { title: '所属办公楼', dataIndex: 'buildingName', slotName: 'buildingName', width: 110 },
  { title: '所属会议室', dataIndex: 'meetingRoomName', slotName: 'meetingRoomName', width: 110 },
  { title: '设备类型', dataIndex: 'userType', slotName: 'userType', width: 110 },
  { title: '设备状态', dataIndex: 'status', slotName: 'status', width: 110 },
  { title: 'SN码', dataIndex: 'snCode', slotName: 'snCode', width: 210 },
  { title: '激活时间', dataIndex: 'activateTime', slotName: 'activateTime', width: 170 },
  { title: '过期时间', dataIndex: 'expireTime', slotName: 'expireTime', width: 170 },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['system:equipment:updateName'])
  }
]

const tableSearch = debounce(search, 500)

// 重置
const reset = () => {
  queryForm.name = ''
  search()
}

// 删除
const _onDelete = (record: EquipmentResp) => {
  return handleDelete(() => deleteEquipment(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const EquipmentSyncModalRef = ref()
// 同步设备
const onSyncEquipment = () => {
  EquipmentSyncModalRef.value.onShow()
}

const EquipmentAddModalRef = ref<InstanceType<typeof EquipmentAddModal>>()
// 新增
const _onAdd = () => {
  EquipmentAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: EquipmentResp) => {
  EquipmentAddModalRef.value?.onUpdate(record.id)
}

// 修改成功回调
const onUpdateSuccess = async (data: { id: number, name: string, meetingRoomId: number }) => {
  try {
    await updateEquipmentName(data)
    Message.success('修改成功')
    search()
  } catch {
    // 处理错误
  }
}

// const EquipmentDetailDrawerRef = ref<InstanceType<typeof EquipmentDetailDrawer>>()
// 详情
// const onDetail = (record: EquipmentResp) => {
//   EquipmentDetailDrawerRef.value?.onDetail(record.id)
// }
</script>

<style lang="scss" scoped></style>
